package com.yanqu.xiuxian.gameserver.manger.elementalbonds.pb;

import com.yanqu.xiuxian.config.elementalbonds.ElementalBondsParam;
import com.yanqu.xiuxian.config.elementalbonds.enums.ElementalBondsDecorationType;
import com.yanqu.xiuxian.config.mall.enums.ShopType;
import com.yanqu.xiuxian.entity.redis.player.PlayerShowData;
import com.yanqu.xiuxian.gameserver.gameplayer.GamePlayer;
import com.yanqu.xiuxian.gameserver.gameplayer.module.elementalbonds.entity.*;
import com.yanqu.xiuxian.gameserver.gameplayer.module.mall.MallModule;
import com.yanqu.xiuxian.gameserver.manger.elementalbonds.ElementalBondsMgr;
import com.yanqu.xiuxian.gameserver.manger.elementalbonds.entity.ElementalBondsBadgeData;
import com.yanqu.xiuxian.gameserver.manger.elementalbonds.entity.ElementalBondsUseKey;
import com.yanqu.xiuxian.pb.common.PlayerInfoPb;
import com.yanqu.xiuxian.pb.cross.system.CrossElementalBonds;
import com.yanqu.xiuxian.pb.elementalbonds.ElementalBonds;
import com.yanqu.xiuxian.pb.player.WatchPlayer;
import com.yanqu.xiuxian.redis.PlayerRedisMgr;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class ElementalBondsPb {
    public static CrossElementalBonds.ChannelElementalBondsUserKeyMsg.Builder parseElementalBondsUserKeyMsg(ElementalBondsUseKey useKey) {
        CrossElementalBonds.ChannelElementalBondsUserKeyMsg.Builder msg = CrossElementalBonds.ChannelElementalBondsUserKeyMsg.newBuilder();
        msg.setPlayerId(useKey.getPlayerId());
        msg.setChannelId(useKey.getChannelId());
        msg.setServerId(useKey.getServerId());
        return msg;
    }

    public static ElementalBonds.ElementalBondsUserDataMsg.Builder parseElementalBondsPlayerDataMsg(GamePlayer player, ElementalBondsPlayerData data, ElementalBonds.ElementalBondsGameOverMsg gameOverMsg) {
        ElementalBonds.ElementalBondsUserDataMsg.Builder builder = ElementalBonds.ElementalBondsUserDataMsg.newBuilder();
        builder.addAllUnlockCard(new ArrayList<>(data.getUnlockCard()));
        builder.setScore(data.getScore());
        int seasonId = ElementalBondsMgr.getInstance().getSeasonId();
        List<Integer> buyBattlePassLevelList = player.getModule(MallModule.class).getBuyBattlePassLevelList(ShopType.ElementalBonds.getValue(), 1);
        if (!buyBattlePassLevelList.isEmpty()) {
            builder.addAllBattlePass(buyBattlePassLevelList);
        }
        builder.setDataSeason(data.getSeasonId());
        builder.setGuide(data.isGuide());
        if (gameOverMsg != null) {
            builder.setOverMsg(gameOverMsg);
        }
        for (ElementalBondsCashMallData mallData : data.getCashMallMap().values()) {
            if (mallData.getBeginTime() == 0 && mallData.getCount() == 0) {
                continue;
            }
            builder.addMall(ElementalBonds.ElementalBondsMallMsg.newBuilder()
                    .setMallId(mallData.getMallId())
                    .setBeginTime(mallData.getBeginTime())
                    .setCanBuyTimes(mallData.getCount())
            );
        }
        //builder.setGuide2(data.getPlayTimes() == 0);
        int unLockSceneScore = ElementalBondsParam.getInstance().getFightSceneOpenLimit();
        builder.setUnlockSceneMode(data.getScore() >= unLockSceneScore); // 开启盘面机制 && 分数达到解锁条件
        builder.addAllTodaySceneMode(ElementalBondsMgr.getInstance().getSceneList());
        int daySurrenderTimes = ElementalBondsParam.getInstance().getDaySurrenderTimes();
        builder.setCanSurrenderTimes(daySurrenderTimes - data.getSurrenderTimes());
        builder.setBuffCollectTimes(data.getWinStreak());
        builder.addAllUnlockSkill(data.getUnlockSkill());
        builder.addAllSelectedSkill(data.getSelectedSkillList());
        builder.setLinkGuide(data.isLinkGuide());
        ConcurrentHashMap<Integer, Integer> decoration = data.getDecoration();
        if (decoration == null) {
            decoration = new ConcurrentHashMap<>();
        }
        builder.setUseBoardSkin(decoration.getOrDefault(ElementalBondsDecorationType.BOARD_SKIN.getType(), 0));
        builder.setUseAnimation(decoration.getOrDefault(ElementalBondsDecorationType.ANIMATION.getType(), 0));
        builder.setUseEffect(decoration.getOrDefault(ElementalBondsDecorationType.EFFECT.getType(), 0));
        builder.addAllEquipEmoji(data.getEmoji());
        return builder;
    }

    public static ElementalBonds.ElementalBondsHonorMomentMsg.Builder parseElementalBondsHonorMomentMsg(ElementalBondsHonorMoment data) {

        ElementalBonds.ElementalBondsHonorMomentMsg.Builder builder = ElementalBonds.ElementalBondsHonorMomentMsg.newBuilder();
        if (data != null) {
            builder.setHonorMomentType(data.getId());
            builder.setValue(data.getValue());
            builder.setTime(data.getTime());
            builder.setRewardIndex(data.getRewardIndex());
        }
        return builder;
    }

    public static ElementalBondsRecordOpponent parseElementalBondsBattleRecordOpponent(ElementalBonds.ElementalBondsOpponentMsg msg) {
        ElementalBondsRecordOpponent data = new ElementalBondsRecordOpponent();
        data.setScore(msg.getScore());
        data.setPlayerId(msg.getUserId());
        data.setChannelId(msg.getChannelId());
        data.setServerId(msg.getServerId());
        if (data.getPlayerId() == 0) {
            data.setName(msg.getPlayerInfo().getNickName());
            data.setHeadIcon(msg.getPlayerInfo().getHeadIcon());
        }
        return data;
    }

    public static ElementalBonds.ElementalBondsBattleRecordMsg.Builder parseElementalBondsBattleRecordMsg(ElementalBondsBattleRecord data) {
        ElementalBonds.ElementalBondsBattleRecordMsg.Builder builder = ElementalBonds.ElementalBondsBattleRecordMsg.newBuilder();
        builder.setNo(data.getNo());
        builder.setTime(data.getTime());
        builder.setOpponent(parseOpponentMsg(data.getOpponentInfo()));
        builder.setOpponentCard(data.getOpponentCardId());
        builder.setOpponentScore(data.getOpponentScore());
        builder.setSelfCard(data.getSelfCardId());
        builder.setSelfScore(data.getSelfScore());
        builder.setWin(data.isWin());
        builder.setScoreChange(data.getScoreChange());
        builder.addAllSelfSkillId(data.getSelfSkills());
        builder.addAllOpponentSkillId(data.getOpponentSkills());
        builder.setType(data.getType());
        return builder;
    }

    public static ElementalBonds.ElementalBondsOpponentMsg.Builder parseOpponentMsg(ElementalBondsRecordOpponent data) {
        ElementalBonds.ElementalBondsOpponentMsg.Builder builder = ElementalBonds.ElementalBondsOpponentMsg.newBuilder();
        builder.setScore(data.getScore());
        builder.setUserId(data.getPlayerId());
        PlayerShowData showData = PlayerRedisMgr.getInstance().getPlayerShowInfo(data.getPlayerId());
        if (showData != null) {
            builder.setPlayerInfo(PlayerInfoPb.buildPlayerBaseDataMsg(showData));
        } else {
            WatchPlayer.PlayerBaseDataMsg.Builder baseData = WatchPlayer.PlayerBaseDataMsg.newBuilder();
            baseData.setPlayerId(0);
            baseData.setNickName(data.getName());
            baseData.setHeadIcon(data.getHeadIcon());
            builder.setPlayerInfo(baseData);
        }
        builder.setChannelId(data.getChannelId());
        builder.setServerId(data.getServerId());
        return builder;
    }

    public static ElementalBonds.ElementalBondsBadgeMsg.Builder parseElementalBondsBadgeMsg(ElementalBondsBadgeData badge) {
        ElementalBonds.ElementalBondsBadgeMsg.Builder builder = ElementalBonds.ElementalBondsBadgeMsg.newBuilder();
        builder.setBadgeId(badge.getBadgeId());
        builder.setScore(badge.getScore());
        builder.setSeasonId(badge.getSeasonId());
        return builder;
    }

    public static ElementalBonds.ElementalBondsTaskDataMsg.Builder parseElementalBondsTaskData(ElementalBondsTaskData taskData) {
        ElementalBonds.ElementalBondsTaskDataMsg.Builder builder = ElementalBonds.ElementalBondsTaskDataMsg.newBuilder();
        builder.setTaskId(taskData.getTaskId());
        builder.setTaskValue(taskData.getTaskProgress());
        builder.setIsReward(taskData.getTaskRewardTime() > 0);
        builder.setUniqueId(taskData.getUniqueId());
        builder.setReward(taskData.getReward());
        return builder;
    }

    public static ElementalBonds.ElementalBondsRedPointData parseElementalBondsRedPointData(ElementalBonds.ElementalBondsRedPointEnum redPointType, boolean isShow) {
        ElementalBonds.ElementalBondsRedPointData.Builder builder = ElementalBonds.ElementalBondsRedPointData.newBuilder();
        builder.setIsShow(isShow);
        builder.setRedPointType(redPointType);
        return builder.build();
    }
}
